#include "kernel/types.h"
#include "kernel/net.h"
#include "kernel/stat.h"
#include "user/user.h"

static void
http(uint16 sport, uint16 dport, int attempts)
{
  int fd;
  char *obuf = "POST /getrp HTTP/1.1\r\nCookie: id=123\r\nContent-Length: 6\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\nage=19";
  uint32 dst;

  // 10.0.2.2, which qemu remaps to the external host,
  // i.e. the machine you're running qemu on.
  dst = (10 << 24) | (0 << 16) | (2 << 8) | (2 << 0);

  
  if((fd = connect(dst, sport, dport, IPPROTO_TCP)) < 0){
    fprintf(2, "ping: connect() failed\n");
    exit(1);
  }

  for(int i = 0; i < attempts; i++) {
    if(write(fd, obuf, strlen(obuf)) < 0){
      fprintf(2, "ping: send() failed\n");
      exit(1);
    }
  }

  char ibuf[1024];
  int cc = read(fd, ibuf, sizeof(ibuf)-1);
  if(cc < 0){
    fprintf(2, "ping: recv() failed\n");
    exit(1);
  }
  ibuf[cc]='\0';
  printf("rec: %s\n", ibuf);
}


int
main(int argc, char *argv[])
{
  uint16 dport = NET_TESTS_PORT;
  http(2000, dport, 1);
  
  exit(0);
}
